//
//  HLGiftContentView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/12.
//

#import "HLGiftContentView.h"
#import <FrameAccessor/FrameAccessor.h>
#import "HLIMKitMessageModel.h"
#import <NIMSDK/NIMMessage.h>
#import "HLGiftAttachment.h"

@implementation HLGiftContentView


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.bubbleImageView.hidden = YES;
        self.backgroundColor = UIColorWhite;
        self.layer.cornerRadius = 8;
        self.layer.masksToBounds = YES;
        
        self.giftImageV = [UIImageView.alloc initWithFrame:CGRectMake(0, 0, 56, 56)];
        [self addSubview:self.giftImageV];
        
        self.giftNameL = [UILabel.alloc init];
        self.giftNameL.textColor = [UIColor colorWithRed:36/255.0 green:41/255.0 blue:51/255.0 alpha:1.0];
        self.giftNameL.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.giftNameL];
        
        self.watchL = [UILabel.alloc init];
        self.watchL.text = @"点击查看";
        self.watchL.font = [UIFont systemFontOfSize:11];
        self.watchL.textColor = [UIColor colorWithRed:251/255.0 green:27/255.0 blue:94/255.0 alpha:1.0];
        [self.watchL sizeToFit];
        [self addSubview:self.watchL];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.giftImageV.left = 2;
    self.giftImageV.centerY = self.middleY;
    
    self.giftNameL.left = 66;
    self.giftNameL.viewSize = CGSizeMake(self.width - 66 - 12, 20);
    self.giftNameL.top = 11;
    
    self.watchL.left = 66;
    self.watchL.top = self.giftNameL.bottom + 4;
}

- (void)refreshData:(HLIMKitMessageModel *)messageModel
{
    [super refreshData:messageModel];
    
    NIMCustomObject *object = (NIMCustomObject *)messageModel.message.messageObject;
    HLGiftAttachment *attachment = (HLGiftAttachment *)object.attachment;
    
    NSString *giftName = attachment.giftName;
    
    NSString *prefixName = @"";
    if ([messageModel.message.from isEqualToString:HLRunTime.shareInstance.mine.userId]) {
        prefixName = @"送出礼物：";
    } else {
        prefixName = @"送你礼物：";
    }
    
    self.giftNameL.text = [NSString stringWithFormat:@"%@%@", prefixName,giftName];
    [self.giftImageV sd_setImageWithURL:[NSURL URLWithString:attachment.giftImgLink] placeholderImage:[UIImage imageNamed:@"imImagePlaceholder"]];
}

@end
